<?php
class Admin_Model_Tabs
{    
    private static $_instance;
    
    private function __construct() {
    }
    
    /**
     * @return Admin_Model_Tabs
     * */
    public static function factory() {
    	if(self::$_instance == null) {
    		self::$_instance = new self();
    	}
    
    	return self::$_instance;
    }
    
    /**
     * Insert tab
     * @param array $data
     * @return 1 on success, 0 on failure
     * */
    public function insert($data) {
        if(!$this->isValid($data)) {
            return 0;
        }
        
        $dao = Admin_Model_DAO_Tabs::factory();
        try {
            $result = $dao->insert($data);
            if($result) {//Log
            	$data['id'] = $result;
            	Utils_Global::storeBackendLog('insert', Admin_Model_DAO_Tabs::$_table, $result, $data);
            }
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $result;
    }
    
    /**
     * Update tabs
     * @param integer $id
     * @param array $data
     * @return true on success, false on failure
    */
    public function update($id, $data) {
        $result = 0;
        if(!$this->isValid($data, true)) {
            return 0;
        }
        
        $dao = Admin_Model_DAO_Tabs::factory();
        try {
        	$result = $dao->update($id, $data);
            if($result) {//Log
            	$data['id'] = $id;
            	Utils_Global::storeBackendLog('update', Admin_Model_DAO_Tabs::$_table, $id,  $data);
            }
        } catch (Exception $exc) {
        	prBinh($exc);
        	Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        return $result;
    }
    
    /**
     * Delete tabs by given $id
     * @param integer $id
     * @return true on success, false on failure
     * */
    public function delete($id) {
        $result = 0;
        if(!$id) {
            return 0;
        }
        
        $dao = Admin_Model_DAO_Tabs::factory();
        try {
            $result = $dao->delete($id);
            if($result) {//Log
            	Utils_Global::storeBackendLog('delete', Admin_Model_DAO_Tabs::$_table, $id, array('id' => $id));
            }
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);  
        }
        
        return $result;
    }
    
    /**
     * Get tabs by given condition
     * @param array $options
     * @return array
     * */
    public function getTabs($options = array()) {
        $tab = array();
        $dao = Admin_Model_DAO_Tabs::factory();
        try {
            $temp = $dao->getTabs($options);
            if(is_array($temp)) {
                foreach ($temp as $t) {
                    if($tab[$t['id']]) {
                        $tab[$t['id']]['titles'][$t['title_lang']] = array('id' => $t['title_tran_id'], 'seo_title' => $t['seo_title'], 'content' => $t['title_content']);
                        $tab[$t['id']]['contents'][$t['content_lang']] = array('id' => $t['content_tran_id'], 'seo_title' => $t['content_seo'], 'content' => $t['content_content']);
                    } else {
                        $tab[$t['id']] = $t;
                        $tab[$t['id']]['titles'] = array($t['title_lang'] => array('id' => $t['title_tran_id'], 'seo_title' => $t['seo_title'], 'content' => $t['title_content']));
                        $tab[$t['id']]['contents'] = array($t['content_lang'] => array('id' => $t['content_tran_id'], 'seo_title' => $t['content_seo'], 'content' => $t['content_content']));
                    }
                }
            }
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $tab;
    }
    
    /**
     * Get courses count by given Id
     * @param array $options
     * @return integer
     * */
    public function getCoursesCount($options = array()) {
        $count = 0;
        $dao = Admin_Model_DAO_Tabs::factory();
        try {
            $count = $dao->getCourseCount($options);
        } catch (Exception $exc) {
            prBinh($exc);
            Utils_Global::storeLog($exc, __FILE__, __LINE__);
        }
        
        return $count;
    }
    
    private function isValid($data, $isUpdate=false) {
        if(!$data['i18n_title'] || !$data['i18n_content']) {
            return false;
        }
        
        return true;
    }
}